define(function (require, exports, module) {
    'use strict';
    //这个是将商品活动进行汇总以后的数据，纬度是活动本身
    var $ = require('jquery'),
        _ = require('underscore'),
        Backbone = require('backbone'),
        notify = require('js/utils/notify'),
        models = require('js/apps/report/models'),
        op = require('js/apps/op/models'),
        utils = require('js/utils/tools'),
        moment = require('moment'),
        chart = require('js/utils/chart'),
        helper = require('js/apps/report/common');

    var ListView = Backbone.View.extend({
        events: {
            'click #search': 'doSearch',
            'click #freshBtn': 'doFresh'
        },
        initialize: function (options) {
            this.options = options;
            this.model = new models.FruitReport();
            this.model.bind('change reset', this.renderWithData, this);
        },
        doSearch: function () {
            this.options = {};
            this.options.date_range = helper.getDateRange();

            this.load();
        },
        doFresh: function () {
            notify.warning('警告', '更新中，耗时较长请等待...');
            var that = this;
            $.post('/admin/report/fresh_fruit/').done(function (resp) {
                var data = resp.data;
                notify.success('提示', '更新成功！');
                that.load();
            }).fail(function (data) {
                notify.notifyResp(data);
            });
        },
        render: function () {
            this.$el.html("");
            $(window).scrollTop(0);
            return this;
        },
        renderWithData: function () {
            this.$el.html(this.template({
                items: this.model.list,
                options: this.options,
            }));
            helper.loadDatePicker({
                dateRange: this.options.date_range
            });
            var that = this,
                idData = {
                    'bet_dist': { title: '投注分布', data: [] },
                    'bet_index_count': { title: '投注商品次数分布', data: [] },
                    'bet_index_amount': { title: '投注商品金额分布', data: [] },
                    'win_index': { title: '开奖结果分布', data: [] }
                },
                amount = [0, 1, 10, 100, 500, 1000];
            var name;
            _.each(amount, function (v) {
                name = 'amount_' + v;
                idData['bet_dist'].data.push({
                    name: v,
                    y: that.model.dist[name] || 0
                })
            });
            for (var i = 0; i < 8; ++i) {
                name = 'index_' + i + '_count'
                idData['bet_index_count'].data.push({
                    name: FRUIT_INDEX[i],
                    y: that.model.dist[name] || 0
                });
                name = 'index_' + i + '_amount'
                idData['bet_index_amount'].data.push({
                    name: FRUIT_INDEX[i],
                    y: that.model.dist[name] || 0
                });
            }
            for (var i = 0; i < 25; ++i) {
                name = 'win_index_' + i;
                if (i != 3 && that.model.dist[name] >0) {
                    idData['win_index'].data.push({
                        name: FRUIT_WIN_INDEX[i],
                        y: that.model.dist[name] || 0
                    })
                }
            }
            _.each(idData, function (v, k) {
                var options = chart.getHighChartPieOptions();
                options.series[0].data = v.data;
                options.title.text = v.title;
                $('#' + k).highcharts(options);
            })
            return this;
        },
        load: function () {
            this.model.fetch({
                reset: true,
                data: {
                    date_range: this.options.date_range.join(',')
                },
                error: function (c, r, o) {
                    notify.notifyResp(r);
                }
            });
        }
    });
    exports.List = Backbone.View.extend({
        el: "#content_wrapper",
        template: "report/Fruit.html",
        render: function () {
            var today = moment().format('YYYY-MM-DD'),
                dateRange = [today, today],
                view = new ListView({
                    date_range: dateRange,
                });
            view.template = this.template;
            this.$el.empty();
            this.$el.append(view.render().el);
            view.load();
            return this;
        }
    });
});
